#!/usr/bin/env bash

set -e

# arch in the rust target
arch="${1}"
shift

if [ -n "${CROSS_DEBUG}" ]; then
    set -x
fi

if [ "${CROSS_RUNNER}" = "" ]; then
    CROSS_RUNNER=qemu-user
fi

# select android abi, and find the shared libc++ library
android_abi="${arch}-linux-android"
qarch="${arch}"
case "${arch}" in
    arm)
        android_abi="arm-linux-androideabi"
        ;;
    i686)
        qarch="i386"
        qemu_args=("-cpu" "n270")
        ;;
    x86_64)
        qemu_args=("-cpu" "qemu64,+mmx,+sse,+sse2,+sse3,+ssse3,+sse4.1,+sse4.2,+popcnt")
        ;;
esac
libdir="/android-ndk/sysroot/usr/lib/${android_abi}"

# Android 5.x doesn't support C++.
if [[ -f "${libdir}/libc++_shared.so" ]]; then
    export LD_PRELOAD="${libdir}/libc++_shared.so"
fi
case "${CROSS_RUNNER}" in
    native)
        exec "${@}"
        ;;
    qemu-user)
        exec "qemu-${qarch}" "${qemu_args[@]}" "${@}"
        ;;
    *)
        echo "Invalid runner: \"${CROSS_RUNNER}\"";
        echo "Valid runners are: native and qemu-user"
        exit 1
        ;;
esac
